<!-- 红包雨活动 -->
<template>
	<scroll-view scroll-y="true">
		<view class="red-envelope-rain" :style="{height:`calc(100vh - ${rainHeight + 100}rpx)`}">
			<view v-for="(redEnvelope, index) in redEnvelopes" :key="index" class="red-envelope"
				:style="{ top: redEnvelope.top + 'px', left: redEnvelope.left + 'px' }"
				@click="handleRedEnvelopeClick(index)"></view>
		</view>
	</scroll-view>
</template>

<script>
	export default {
		props:{
			rainHeight:{
				type:Number,
				default:0
			}
		},
		data() {
			return {
				redEnvelopes: [],
				redEnvelopeInterval: null,
				time:null,
			}
		},
		mounted() {
			// 每秒创建一个红包
			this.time = setInterval(this.initializeRedEnvelopes, 300); 
			// 更新红包位置，约 60 帧
			setInterval(this.moveRedEnvelopes, 1000 / 60); 
		},
		beforeDestroy() {
			this.stopRedEnvelopeRain();
		},
		methods: {
			clearsetInte(){
				clearInterval(this.time)
				this.redEnvelopes = []
			},
			initializeRedEnvelopes() {
				const numRedEnvelopes = 100; // 红包数量
				// for (let i = 0; i < numRedEnvelopes; i++) {
				const redEnvelope = {
					id: Date.now(),
					top: 0, // 随机纵坐标
					left: Math.random() * (uni.getSystemInfoSync().windowWidth - 70), // 随机横坐标
					speed: Math.random() * 6 + 1, // 随机速度
				};
				this.redEnvelopes.push(redEnvelope);
                // console.log(Math.floor(Math.random() * numRedEnvelopes));
				setTimeout(() => {
					this.redEnvelopes = this.redEnvelopes.filter(p => p.id !== redEnvelope.id);
				}, 4000); // 4秒后移除红包
			},
			// startRedEnvelopeRain() {
			// 	this.redEnvelopeInterval = setInterval(() => {
			// 		this.moveRedEnvelopes();
			// 	}, 1000 / 60); // 每秒60帧
			// },
			stopRedEnvelopeRain() {
				clearInterval(this.redEnvelopeInterval);
			},
			moveRedEnvelopes() {
				this.redEnvelopes.forEach((redEnvelope, index) => {
					// console.log(redEnvelope, "redEnvelopes")
					redEnvelope.top += redEnvelope.speed;
					if (redEnvelope.top > uni.getSystemInfoSync().windowHeight) {
						this.redEnvelopes = this.redEnvelopes.filter(p => p.id !== redEnvelope.id);
					}
				});
			},
			// 处理红包点击事件，可以增加分数或显示提示等操作
			handleRedEnvelopeClick(index) {
				// 例如：this.score += 1;
				// 或者：this.showTip = true;
				// 可以根据实际需求自行添加逻辑
				this.redEnvelopes.splice(index, 1); // 点击后移除红包
			},
		}
	}
</script>

<style lang="scss">
	.red-envelope-rain {
		position: relative;
		overflow: hidden;
		width: 100vw;
	}

	.red-envelope {
		position: absolute;
		background-image: url('../../image/xiaohb.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;
		width: 200rpx;
		height: 200rpx;
	}
</style>
